package com.gdth.widget.dialog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.gdth.R;
import com.gdth.widget.wheel.WheelView;
import com.gdth.widget.wheel.adapter.ArrayWheelAdapter;
import com.gdth.widget.wheel.adapter.WheelAdapterForList;
import com.gdth.widget.wheel.listener.OnWheelChangedListener;

import android.app.Dialog;  
import android.content.Context;  
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;  
import android.view.View;  
import android.widget.TextView;

public class WheelViewDialogTwo extends Dialog {
	
    public WheelViewDialogTwo(Context context) {  
        super(context);  
    }  
  
    public WheelViewDialogTwo(Context context, int theme) {  
        super(context, theme);  
    }  
  
    public static class Builder {  
        private Context context;  
        private String title; 
        private Bundle bundle;
        
        private DialogInterface.OnClickListener positiveButtonClickListener;  
        private DialogInterface.OnClickListener negativeButtonClickListener;    
        
        private TextView btn_take_select, btn_cancel;  

        /** 
         * 第一个的WheelView控件 
         */  
        private WheelView wheelOne;  
        /** 
         * 第二个的WheelView控件 
         */  
        private WheelView wheelTwo;  
      
        /** 
         * 第一个WheelView的数据源
         */  
        private String[] oneDatas = new String[]{};   
        
        /** 
         * 第二个WheelView的数据源
         */  
        private Map<String, String[]> twoDatasMap = new HashMap<String, String[]>();  

        /** 
         * 第一个WheelView的值
         */  
        private String sCurrentOne; 
        public int pCurrent;
        
        /** 
         * 第二个WheelView的值
         */  
        public String sCurrentTwo ="";
        
        public int station = 0;
        
        public final static int RESULT_CODE=1; 
        
    	
  
        public Builder(Context context) {  
            this.context = context;  
        }  

  
        /** 
         * Set the Dialog title from resource 
         * @param title 
         * @return 
         */  
        public Builder setTitle(int title) {  
            this.title = (String) context.getText(title);  
            return this;  
        }  
  
        /** 
         * Set the Dialog title from String 
         * @param title 
         * @return 
         */  
        public Builder setTitle(String title) {  
            this.title = title;  
            return this;  
        }  
        
        public Builder setBundle(Bundle bundle) {  
            this.bundle = bundle;  
            return this;  
        }
  
        public Builder setPositiveButton(int positiveButtonText,  
                DialogInterface.OnClickListener listener) {  
            this.positiveButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setPositiveButton(String positiveButtonText,  
                DialogInterface.OnClickListener listener) {  
            this.positiveButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setNegativeButton(int negativeButtonText,  
                DialogInterface.OnClickListener listener) {
            this.negativeButtonClickListener = listener;  
            return this;  
        }  
  
        public Builder setNegativeButton(String negativeButtonText,  
                DialogInterface.OnClickListener listener) {  
            this.negativeButtonClickListener = listener;  
            return this;  
        }
  
        public WheelViewDialogTwo create() {  
        	 oneDatas = this.bundle.getStringArray("oneDatas");
             
             Bundle bundle = this.bundle.getBundle("twoDatasMap");  
             Set<String> keySet = bundle.keySet();   // 得到bundle中所有的key  
             Iterator<String> iter = keySet.iterator();  
             while(iter.hasNext()) {  
                 String key = iter.next();  
                 twoDatasMap.put(key, bundle.getStringArray(key));  
             }
             
            LayoutInflater inflater = (LayoutInflater) context  
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
            
            final WheelViewDialogTwo dialog = new WheelViewDialogTwo(context, R.style.style_dialog);
            
            View layout = inflater.inflate(R.layout.wheelview_two, null);  
            
            TextView txt_title = (TextView) layout.findViewById(R.id.lv_item_title);
            txt_title.setText(title);
            btn_take_select = (TextView) layout.findViewById(R.id.main_btn_save);   
            btn_cancel = (TextView) layout.findViewById(R.id.main_btn_menu);  
            
            if (positiveButtonClickListener != null) {  
            	btn_take_select.setOnClickListener(new View.OnClickListener() {  
                            public void onClick(View v) {  
                                positiveButtonClickListener.onClick(dialog,  
                                        DialogInterface.BUTTON_POSITIVE);  
                            }  
                        });  
            }  
            if (negativeButtonClickListener != null) {  
            	btn_cancel.setOnClickListener(new View.OnClickListener() {  
                            public void onClick(View v) {  
                                negativeButtonClickListener.onClick(dialog,  
                                        DialogInterface.BUTTON_NEGATIVE);  
                            }  
                        });  
            }  
            
            layout.setOnClickListener(new View.OnClickListener() {  
                            public void onClick(View v) {  
                                negativeButtonClickListener.onClick(dialog,  
                                        DialogInterface.BUTTON_NEGATIVE);  
                            }  
                        });
//    		
            wheelOne = (WheelView) layout.findViewById(R.id.wheel_view_one);  
            wheelTwo = (WheelView) layout.findViewById(R.id.wheel_view_two);  
            
            wheelOne.setViewAdapter(new ArrayWheelAdapter<String>(this.context, oneDatas));
            
            wheelOne.addChangingListener(onWheelChange); 
            wheelTwo.addChangingListener(onWheelChange);  
      
            wheelOne.setVisibleItems(1);  
            wheelTwo.setVisibleItems(1);   
            updateWheelTwo();  
            
            dialog.setContentView(layout);  
            return dialog;  
        }  
        
        /** 
         * 根据当前的市，更新区WheelView的信息 
         */  
        private void updateWheelTwo()
        {  
            pCurrent = wheelOne.getCurrentItem();  
            sCurrentOne = oneDatas[pCurrent];  
            String[] areas = twoDatasMap.get(sCurrentOne);  
            
            sCurrentTwo = twoDatasMap.get(sCurrentOne)[0];  
            
            if (areas == null)  
            {  
                areas = new String[] { "" };  
            }  
            wheelTwo.setViewAdapter(new WheelAdapterForList<String>(this.context, areas));  
            wheelTwo.setCurrentItem(0);  
        }  
        
        /**
    	 * 时间控件点击事件
    	 */
    	private OnWheelChangedListener onWheelChange = new OnWheelChangedListener(){

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				if (wheel == wheelOne)  {  
	            	updateWheelTwo();
	            } 
				else if (wheel == wheelTwo)  {  
	            	sCurrentTwo = twoDatasMap.get(sCurrentOne)[newValue]; 
	            }  
			}
    		
    	};
    }  
}  
